<?php
namespace app\admin\model;

use think\Model;
use think\Config;

class Courses extends Model
{
    //连接趁早数据库
    protected $connection = 'cz_db_config';

    /**
     * 根据单字段获取信息
     * @param $user_name
     * @param string $fields
     * @return object
     */
    public function getInfoByField($field_name, $field_value, $fields = '*')
    {
        return $this->where($field_name, $field_value)->field($fields)->find();
    }

    /**
     * 获取课程列表
     * @return array
     */
    /*public function getAllLists(array $where = [])
    {
        $status = [
            1 => '启用',
            2 => '停用',
        ];
        $coursesClassifyModel = new CoursesClassify();
        $lists = $this->where($where)->order('id desc')->paginate(50, false, ['query'=>request()->param()]);
        $lists_arrsy = [];
        foreach ($lists as $li){
            $lists_arrsy[] = [
                'id'              => $li->id,
                'courses_title'   => $li->courses_title,
                'courses_content' => $li->courses_content,
                'h5_link'         => $li->h5_link,
                'banner'          => $li->banner,
                'classify_name'   => $coursesClassifyModel->getFileByField('id', $li->classify_id, 'classify_name'),
                'price'           => $li->price,
                'creata_time' => $li->creata_time,
                'generalize_link' => $li->generalize_link,
                'generalize_earning' => $li->generalize_earning,
                'status'          => $status[$li->status],
                'status_code' => $li->status,
                'has_earning' => $li->has_earning,
                'is_default_banner' => $li->is_default_banner,
                'has_generalize'    => $li->has_generalize
            ];
        }
        return [
            'lists' => $lists_arrsy,
            'page'  => $lists->render()
        ];
    }*/
    public function getAllLists(array $where = [])
    {
        $where['status'] = 'published';     //已发布状态
        $where['cps_is_del'] = 0;           //cps未删除状态
        $coursesCategoryRelationModel = new CoursesCategoryRelation();
        if ($where['classify_id']) {
            $category_id = $where['classify_id'];
            unset($where['classify_id']);
            $course_id = $coursesCategoryRelationModel->getCourseIdByCategoryId($category_id);
            if ($course_id) {
                $where['id'] = ['in', $course_id];
            }
        }
        $field = 'id, title, price, smallPicture, createdTime, cps_generalize_link, cps_generalize_earning, cps_banner, cps_generalize_link, cps_is_default_banner';
        $lists = $this->where($where)->field($field)->order('id desc')->paginate(50, false, ['query'=>request()->param()]);
        $lists_array = [];
        foreach ($lists as $li){
            $lists_array[] = [
                'id'              => $li->id,
                'title'           => $li->title,
                'h5_link'         => '',
                'banner'          => $li->cps_banner ? $li->cps_banner : $li->smallPicture,
                'classify_name'   => $coursesCategoryRelationModel->getCategoryByCourseId($li->id),
                'price'           => $li->price,
                'creata_time'     => date('Y-m-d H:i:s', $li->createdTime),
                'generalize_link' => $li->cps_generalize_link,
                'generalize_earning' => $li->cps_generalize_earning,
                'has_earning'       => $li->cps_generalize_earning > 0.00 ? '是' : '否',
                'is_default_banner' => $li->cps_is_default_banner ? '是' : '否',
                'has_generalize'    => $li->cps_generalize_link ? '是' : '否'
            ];
        }
        return [
            'lists' => $lists_array,
            'page'  => $lists->render()
        ];
    }

    /**
     * 修改数据
     * @param array $where
     * @param array $save
     * @return int
     */
    public function updateData(array $where = [], array $save = [])
    {
        return $this->where($where)->update($save);
    }

    /**
     * 获取课程内容
     * @param $course_id
     * @return array
     */
    public function getInfoById($course_id)
    {
        $info = $this->where([
            'id' => $course_id
        ])->find()->toArray();
        $info['bannel'] = $info['cps_banner'] ? $info['cps_banner'] : $info['smallPicture'];
        return $info;
    }
    /**
     * 根据课程名获取课程ID
     * @param $category_id
     * @return string
     */
    public function getCourseIdByTitle($title)
    {
        $ids = $this->where([
            'title' => ['like', '%'.$title.'%'],
        ])->column('id');
        return implode(',', $ids);
    }
}